package com.honey.compilation.sql.ddl;

import java.util.ArrayList;
import java.util.List;

import com.honey.compilation.sql.SqlColumn;
import com.honey.compilation.sql.SqlKeyWord;
import com.honey.compilation.sql.SqlScript;
import com.honey.compilation.sql.SqlTable;
import com.honey.core.builder.CharacterBuilder;
import com.honey.core.builder.CharacterBuilderFactory;
import com.honey.core.compiler.CompilationException;
import com.honey.core.compiler.IndentSpace;
import com.honey.core.utils.StringUtility;

/**
 * 在SQL标准规范中(SQL92 SQL99 和SQL2003)没有定义创建索引语法, 这里的命名不一定能够在所有的数据库中执行
 * 创建索引命令: ALERT TABLE ADD INDEX 
 * @author Administrator
 *
 */
@Deprecated
public  class AlterTableAddIndex extends AlterTableAdd implements SqlScript{
	
	
	private IndexType indexType;
	
	private List<String> columns ;
	
	/**
	 * 
	 * @param tableName 表的名称
	 */
	public AlterTableAddIndex(String tableName) {
		super(tableName);	
		columns = new ArrayList<String>();
	}
	
	/**
	 * 构造函数
	 * @param table 数据库表对象
	 */
	public AlterTableAddIndex(SqlTable table){
		super(table);
		columns = new ArrayList<String>();
	}
	
	/**
	 * 
	 * @param tableName 表的名称
	 * @param columns 创建索引的列(索引的名称就是字段名称 )
	 */
	public AlterTableAddIndex(String schema, String tableName) {
		super(schema, tableName);
		columns = new ArrayList<String>();
	}
	
	public AlterTableAddIndex(String tableName,SqlColumn ...columns) {
		this(tableName);	
		addColumn(columns);
	}
	
	public void addColumn(String ...columns){
		for(String column : columns)
			if(StringUtility.stringHasValue(column))this.columns.add(column);
	}
	
	public void addColumn(SqlColumn ...columns){
		for(SqlColumn column : columns)
			if(column != null)this.columns.add(column.getName().getValue());
	}
	
	public IndexType getIndexType() {
		return indexType;
	}
	
	public void setIndexType(IndexType indexType) {
		this.indexType = indexType;
	}

	@Override
	public CharacterBuilder compiledContent(int indentLevel ) throws CompilationException {
		CharacterBuilder answer = CharacterBuilderFactory.createC16StringBuilder() ;
		
		boolean newLine = false;
		for(String column : columns ){
			if (newLine)IndentSpace.newLine(answer);else newLine = true;
			
			answer.append( super.compiledContent( indentLevel ) ) ; 
//			if(indexType == IndexType.FULLTEXT){
//				answer.append( IndexType.FULLTEXT );
//				
//			}else if(indexType == IndexType.UNIQUE){
//				answer.append( IndexType.UNIQUE );
//			} 
			switch( indexType ){
				case FULLTEXT:	
					answer.append( IndexType.FULLTEXT );
					break;
				case UNIQUE:
					answer.append( IndexType.UNIQUE );
					break;
			}
			
			
			answer.append( SqlKeyWord.INDEX )
			.append( SqlKeyWord.OPERATION_PAREN_LEFT )
			.append( column )
			.append( SqlKeyWord.OPERATION_PAREN_RIGHT )
			
			.append(SqlKeyWord.OPERATION_SEMICOLON);
		}
		
		return answer ;
	}
	
	
	public enum IndexType{
		
		/**
		 * 默认
		 */
		NORMAL("Normal"),
		
		/**
		 * 唯一索引
		 */
		UNIQUE("UNIQUE"),
		
		/**
		 * 全文检索索引
		 */
		FULLTEXT("FULLTEXT"),
		
		;
		
		private final String name ;
		
		private IndexType(String name){
			this.name =  name;
		}
		
		public String getName() {
			return name;
		}
		
		@Override
		public String toString() {
			return name + SqlKeyWord.OPERATION_SPACE ;
		}
	}
}
